from typing import Any, Annotated, Optional, List
from datetime import timedelta,datetime
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
from fastapi import APIRouter, Depends, HTTPException, status, Path
from sqlalchemy.orm.session import Session
from common.deps import check_jwt_token, get_db
from core.response import response_code
from datetime import datetime
from setting.web import settings
from core.schemas.course import CourseCreate
import crud

router = APIRouter()


@router.post("/create", summary="添加课程")
async def create_ccourse(new_course:CourseCreate,db: Session = Depends(get_db),payload=Depends(check_jwt_token)):
    uid = payload.get("uid")
    res = crud.course.add_course(db,new_course.name,uid,new_course.times,new_course.class_id)
    for i in new_course.reservation:
        reservation_res = crud.reservation.create_c(db,
                                  i.classroomId,
                                  'class',
                                  uid,
                                  i.get_times_as_datetimes()[0],
                                  i.get_times_as_datetimes()[1],
                                  f'{new_course.name}-{i.index}',
                                  res.course_id)
        # for i in reservation_res:
        crud.participant.create(db,reservation_res.reservation_id,[x.user_id for x in crud.xclass.list_class_student(db,1,1000,new_course.class_id)[0]])
    return response_code.resp_200(data=res)

@router.get("/list", summary="列出课程")
async def list_course(page: int = 1, pageSize: int = 20,  db: Session = Depends(get_db),payload=Depends(check_jwt_token)):
    res = crud.course.list_course(db,page,pageSize)
    return response_code.resp_200(data=res)

@router.get("/listById", summary="列出我的课程")
async def list_course_by_id(page: int = 1, pageSize: int = 20, teacher_id:int = 1, db: Session = Depends(get_db),payload=Depends(check_jwt_token)):
    res = crud.course.list_course_by_id(db,page,pageSize,teacher_id)
    return response_code.resp_200(data=res)

@router.get("/getCourse", summary="获取课程")
async def get_courses(course_id: int,  db: Session = Depends(get_db),payload=Depends(check_jwt_token)):
    res = crud.course.get_course(db,course_id)
    return response_code.resp_200(data=res)

@router.get("/listRecord", summary="获取上课记录")
async def list_record(page: int = 1, pageSize: int = 20,  db: Session = Depends(get_db),payload=Depends(check_jwt_token)):
    uid = payload.get("uid")
    res = crud.course.list_course_record(db,page,pageSize,uid)
    return response_code.resp_200(data=res)

@router.get("/listRecordByName", summary="获取上课记录")
async def list_record_by_name(page: int = 1, pageSize: int = 20, courseName:str = '',  db: Session = Depends(get_db),payload=Depends(check_jwt_token)):
    uid = payload.get("uid")
    res = crud.course.list_course_record_by_name(db,page,pageSize,uid,courseName)
    return response_code.resp_200(data=res)


@router.get("/listParticipation", summary="获取考勤")
async def list_participation(page: int = 1, pageSize: int = 20, reservation_id:int = 1,  db: Session = Depends(get_db),payload=Depends(check_jwt_token)):
    # uid = payload.get("uid")
    res = crud.reservation.list_participation_by_id(db,page,pageSize,reservation_id)
    return response_code.resp_200(data=res)

@router.get("/listAttendance", summary="获取所有课程考勤数据")
async def list_attendance(db: Session = Depends(get_db),startTime: Optional[str] = None,page:int = 1,pageSize:int  =20,payload=Depends(check_jwt_token)):
    uid = payload.get("uid")
    if startTime == None :
        res = crud.participant.list_classes_attendance(db,page,pageSize,uid)
    else:
        converted_time = datetime.strptime(startTime, "%Y-%m-%d %H:%M")
        res = crud.participant.list_classes_attendance_by_time(db,page,pageSize,uid,converted_time)
    return response_code.resp_200(data=res)
